package com.etrans.util;


import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import com.etrans.http.ParamBean;
/**
 * 
 * @author 劳
 *
 */
public class ThreeDES {
	 // 密钥  
    private final static String secretKey = "fontone-trans@lx100$#365#$";  
    // 车主共赢密钥  
    private final static String shareSecretKey = "foton-trans@lx100$#365#$";  
    // 向量  
    private final static String iv = "20161201";  
    // 加解密统一使用的编码方式  
    private final static String encoding = "utf-8";  
  
    /** 
     * 3DES加密 
     *  
     * @param plainText 普通文本 
     * @return 
     * @throws Exception  
     */  
    public static String encode(String plainText) throws Exception {  
        Key deskey = null;  
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
        return Base64.encode(encryptData);  
    }  
  
    /** 
     * 3DES解密 	
     *  
     * @param encryptText 加密文本 
     * @return 
     * @throws Exception 
     */  
    public static String decode(String encryptText) throws Exception {  
        Key deskey = null;  
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
  
        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));  
  
        return new String(decryptData, encoding);  
    } 
    
    
    /** 
     * 车主共赢3DES加密 
     *  
     * @param plainText 普通文本 
     * @return 
     * @throws Exception  
     */  
    public static String encodeShare(String plainText) throws Exception {  
        Key deskey = null;  
        DESedeKeySpec spec = new DESedeKeySpec(shareSecretKey.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
        return Base64.encode(encryptData);  
    }  
    
    /** 
     * 车主共赢3DES解密 	
     *  
     * @param encryptText 加密文本 
     * @return 
     * @throws Exception 
     */  
    public static String decodeShare(String encryptText) throws Exception {  
        Key deskey = null;  
        DESedeKeySpec spec = new DESedeKeySpec(shareSecretKey.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
  
        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));  
  
        return new String(decryptData, encoding);  
    } 
    
    public static void main(String[] args) {
//    	String k="粤测0123"; 
//    	k = JSONUtils.toJSON(k);
    	String k;
    	try {
    		//String s = "fDJrEG8nlSejlLzigNLPK3IXD5tMyaDhoKT7hKl3JHP7dFn8c74zH2PxANsu hDHXjGr5fnyCRxWr2OSuW6m2k3FdK9nTkmEVh4d5FnYVzyJASJd7vKgx60Vt dyBY3pqzsB1iS3d2HVk8XD4bGT0BezoRr08/km7KF1YQgcPHjYnCV8XbBk+U ZiF+zw4qbfIx15hB9vvlYVvCW8gtg8OO/w==";
    		//String json = ThreeDES.decode(s);
    		//System.out.println(json);
    		String longtime = System.currentTimeMillis()+"";
    		k="10586300,06,,"+longtime;
//    		ParamBean paramBean = (ParamBean)JSONUtils.fromJson(json, ParamBean.class);
//    		System.out.println(paramBean.getLimit());
    		//String st =  JSONUtils.fromJson(json, String.class);
//    		System.out.println(st);
   		System.out.println("加密前数据:"+k);
    	//String encode = ThreeDES.encodeShare(k);
//    		
   		String encode = "IokshyGJgA2eQjbmbq7QrXELU4mAB2U2fNsHW91uYc4=";
   		System.out.println("加密后数据:"+encode);
    		String decode = ThreeDES.decodeShare(encode);
 		System.out.println("解密后的数据："+decode);
//    		String s = JSONUtils.fromJson(decode, new String().getClass());
//    		System.out.println(s);
    	} catch (Exception e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
}
